package org.example.myleet.p661;

public class Solution {

    private static final int[][] DIRECTIONS = new int[][]{
            {-1, 0},
            {-1, 1},
            {0, 1},
            {1, 1},
            {1, 0},
            {1, -1},
            {0, -1},
            {-1, -1},
    };

    public int[][] imageSmoother(int[][] img) {
        int m = img.length, n = img[0].length;
        int[][] smoothImg = new int[m][n];
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                int sum = img[i][j], i_, j_;
                int f = 1;
                for (int k = 0; k < 8; ++k) {
                    i_ = i + DIRECTIONS[k][0];
                    j_ = j + DIRECTIONS[k][1];
                    if (0 <= i_ && i_ < m && 0 <= j_ && j_ < n) {
                        sum += img[i_][j_];
                        ++f;
                    }
                }
                smoothImg[i][j] = sum / f;
            }
        }
        return smoothImg;
    }
}
